<?php

namespace app\commands;

use app\models\Group;
use app\models\GroupUser;
use app\models\User;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\db\Exception;
use yii\helpers\FileHelper;

class HssController extends Controller {

    /**
     * @throws Exception
     */
    public function actionIndex() {
//        $path = RUNTIME_ROOT . '/files';
        $path = FILE_ROOT;
        $files = FileHelper::findFiles($path);
        foreach ($files as $file) {
            if (strpos($file, 'info') !== false) {
                User::parseFile($file);
            }
            if (strpos($file, 'group') !== false) {
                Group::parseFile($file);
            }
        }
        return ExitCode::OK;
    }

    public function actionClear(){
        \Yii::$app->db->createCommand('delete from ii_user')->execute();
        \Yii::$app->db->createCommand("update sqlite_sequence SET seq = 0 where `name` = 'ii_user'")->execute();
        \Yii::$app->db->createCommand('delete from ii_group')->execute();
        \Yii::$app->db->createCommand("update sqlite_sequence SET seq = 0 where `name` = 'ii_group'")->execute();
        \Yii::$app->db->createCommand('delete from ii_group_user')->execute();
        \Yii::$app->db->createCommand("update sqlite_sequence SET seq = 0 where `name` = 'ii_group_user'")->execute();
        return ExitCode::OK;
    }
}